package com.sp.scmdb.webservice.serviceImpl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.sp.scmdb.bean.AreaBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.AreaDao;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.spring.ApplicationContext;
import com.sp.scmdb.webservice.model.AreaModel;
import com.sp.scmdb.webservice.model.ReturnResponse;
import com.sp.scmdb.webservice.service.UploadAreaService;
import com.sp.scmdb.webservice.service.UploadDataService;
import com.sp.scmdb.webservice.suport.CastorSerializer;

@Service("uploadAreaService")
public class UploadAreaServiceImpl implements UploadAreaService {

	private static Log log = LogFactory.getLog(UploadAreaServiceImpl.class);
	private AreaDao areaDao;

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public AreaBean queryAll(AreaBean areaBean, AreaBean oldAreaBean){
		try {
			List<Area> lists = Lists.newArrayList();
			if (areaBean.equals(oldAreaBean)) {
				lists = oldAreaBean.getAllList();
			} else {
				StringBuilder Hql = new StringBuilder("from Area where 1=1");
				if (StringUtils.isNotEmpty(areaBean.getName())) {
					Hql.append(" and name like '%" + areaBean.getName().trim() + "%'");
				}
				if (StringUtils.isNotEmpty(areaBean.getType())) {
					Hql.append(" and type='" + areaBean.getType() + "'");
				}
				lists = areaDao.findAreaByHql(Hql.toString());
			}
			if (CollectionUtils.isNotEmpty(lists)) {
				areaBean.setAllList(lists);
				areaBean.setList(lists.subList(areaBean.getCurrentRow(), areaBean.getLastRow()));
			}
		} catch (Exception e) {
			log.error("queryAll failed! reason:" + e.getMessage());
		}
		return areaBean;
	}

	public String uploadArea(){
		String result = CommonParamter.CHECK_FAIL;
		try {
			List<AreaBean> list = new ArrayList<AreaBean>();
			AreaBean bean = null;
			List<Area> lists = areaDao.queryAll();
			for (Area area : lists) {
				bean = new AreaBean();
				bean.setId(area.getId().toString());
				bean.setName(area.getName());
				bean.setTypeName(CommonParamter.TypeCommon.getAreaType().get(area.getType()));
				bean.setAreaX(area.getAreaX());
				bean.setAreaY(area.getAreaY());
				bean.setReaders(area.getReaders());
				bean.setPhone(area.getPhone());
				list.add(bean);
			}
			
			AreaModel model = new AreaModel();
			model.setListArea(list);			

			model.setUserId(CommonParamter.userID);
			model.setPassword(CommonParamter.password);
			
			CastorSerializer serializerTool = new CastorSerializer();
			ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
			serializerTool.addMappingLocation("com/sp/scmdb/webservice/mapping/mapping.xml");
			serializerTool.marshal(model, byteOutputStream, "UTF-8");
			String str = new String(byteOutputStream.toByteArray());
			
			UploadDataService upload=(UploadDataService )ApplicationContext.getBean("uploadService");
			result = upload.uploadArea(str);			

			// 获取返回值
			ReturnResponse response = (ReturnResponse)serializerTool.unmarshal(new ByteArrayInputStream(result.getBytes()), "GB2312");
			result = response.getDescription();
		} catch (javax.xml.ws.WebServiceException e){
			log.error("uploadArea failed! reason:" + e.getMessage());
			if ("Could not send Message.".equals(e.getMessage())) {
				result = "无法连接到服务器！";
			}
		} catch (Exception e) {
			log.error("uploadArea failed! reason:" + e.getMessage());
			result = "未知错误！";
		}
		return result;
	}
	@Required
	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}	
}
